<template>
  <div class="pagination">
    <!-- 禁用条件 -->
    <button :disabled="pageNo === 1 " @click="$emit('newPageNo' , pageNo - 1)" >上一页</button>
    <!-- 第一页的隐藏和点击 -->
    <button 
      :class="{active: pageNo === 1}" 
      v-show="startEndPage.start > 1"
      @click="$emit('newPageNo' , 1)"
    >1</button>

    <!-- ...的隐藏 -->
    <button v-show="startEndPage.start > 2 ">···</button>

    <button 
      v-for="page in startEndArr" 
      :key="page" 
      :class="{active: pageNo === page }"
      @click="$emit('newPageNo' , page)"
    >{{page}}</button>

     <!-- ...的隐藏 -->
    <button v-show="this.startEndPage.end < totalPages - 1 ">···</button>

    <!-- 最后一页的隐藏和点击 -->
    <button 
      :class="{active: pageNo === totalPages}"
      v-show="startEndPage.end < totalPages"
      @click="$emit('newPageNo' , totalPages)"
    >{{totalPages}}</button>


    <!-- 禁用条件 -->
    <button :disabled="pageNo === totalPages" @click="$emit('newPageNo' , pageNo + 1)">下一页</button>
    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: ["pageNo", "totalPages", "continuepage", "total"],

  computed: {
    /* 
      永远不要不v-for 和 v-if 同时用在同一个元素上
      - 因为v-for的优先级高于v-if
      - 先遍历生成结构,然后再判断去掉某些结构,对性能来说不友好
      - 解决方法:1.把v-for遍历的数据用计算数据先行过滤   2. 多增添一层结构分开书写v-if和v-for
    */
    //根据起始页计算出要遍历的连续页
    startEndArr() {
      //定义一个空数组
      let arr = [];
      //遍历结束页
      for (let i = 1; i <= this.startEndPage.end; i++) {
        //取出需要添加到页面的数字
        if (i >= this.startEndPage.start) {
          arr.push(i);
        }
      }

      return arr;
    },

    /* 
      pageNo: 当前页
      totalPages: 总页数
      continuepage: 连续页
    */
    //计算出起始页
    startEndPage() {
      //拿到this中的数据
      const { pageNo, totalPages, continuepage } = this;
      //定义初始值
      let start,
        end = "";

      //1.判断如果总页数小于等于连续页 则起始页为1，结束页为总页数
      if (totalPages <= continuepage) {
        (start = 1), (end = totalPages);
      } else {
        //2.如果连续页大于总页数计算起始页
        start = pageNo - (continuepage - 1) / 2;
        end = pageNo + (continuepage - 1) / 2;

        //3.如果起始页小于1则 起始页为1 结束页为连续页
        if (start < 1) {
          start = 1;
          end = continuepage;
        }

        //4.如果结束页大于总页数则 结束页为总页树 起始页为总页树-连续页+1
        if (end > totalPages) {
          end = totalPages;
          start = totalPages - continuepage + 1;
        }
      }

      return { start, end };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>